package controllers

import (
    "encoding/json"
    "github.com/beego/beego/v2/server/web"
    "strconv"
    "volux/utils"
    "volux/services"
    "github.com/beego/beego/v2/core/logs"
)

type NamespaceController struct {
    web.Controller
}

type Namespace struct {
    NSID     uint32 `json:"nsid"`
    BdevName string `json:"bdev_name"`
}

// AddNamespace 添加命名空间到子系统
func (c *NamespaceController) AddNamespace() {
    nqn := c.Ctx.Input.Param(":nqn")
    if nqn == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN is required")
        return
    }

    var ns Namespace
    if err := json.Unmarshal(c.Ctx.Input.RequestBody, &ns); err != nil {
        utils.ErrorResponse(c.Controller, 400, "Invalid request body")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    // 生成NGUID和EUI64 (可选)
    nguid := utils.GenerateNGUID()
    eui64 := utils.GenerateEUI64()

    // 创建命名空间
    err := spdkClient.AddNamespaceToSubsystem(nqn, ns.BdevName, ns.NSID, nguid, eui64)
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to add namespace: "+err.Error())
        return
    }

    // 保存到etcd
    storageService := services.NewStorageService(utils.GetEtcdClient())
    if err := storageService.StoreNamespace(nqn, ns.NSID, ns); err != nil {
        // 回滚SPDK操作
        spdkClient.RemoveNamespaceFromSubsystem(nqn, ns.NSID)
        utils.ErrorResponse(c.Controller, 500, "Failed to persist namespace")
        return
    }

    utils.SuccessResponse(c.Controller, ns)
}

// DeleteNamespace 从子系统删除命名空间
func (c *NamespaceController) DeleteNamespace() {
    nqn := c.Ctx.Input.Param(":nqn")
    nsidStr := c.Ctx.Input.Param(":nsid")
    if nqn == "" || nsidStr == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN and NSID are required")
        return
    }

    nsid, err := strconv.ParseUint(nsidStr, 10, 32)
    if err != nil {
        utils.ErrorResponse(c.Controller, 400, "Invalid NSID")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    // 从SPDK删除
    if err := spdkClient.RemoveNamespaceFromSubsystem(nqn, uint32(nsid)); err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to remove namespace: "+err.Error())
        return
    }

    // 从etcd删除
    storageService := services.NewStorageService(utils.GetEtcdClient())
    if err := storageService.DeleteNamespace(nqn, uint32(nsid)); err != nil {
        logs.Warning("Failed to delete namespace from etcd: %v", err)
    }

    utils.SuccessResponse(c.Controller, map[string]string{
        "message": "Namespace removed successfully",
    })
}

func (c *NamespaceController) GetNamespaces() {
    targetNQN := c.Ctx.Input.Param(":nqn")
    if targetNQN == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN is required")
        return
    }

    // TODO: 使用 targetNQN 实现获取命名空间列表的逻辑
    utils.SuccessResponse(c.Controller, map[string]interface{}{
        "nqn": targetNQN,
        "namespaces": []Namespace{},
    })
}
